package club.banyuan.SpringBootHrms.controller;

import club.banyuan.SpringBootHrms.dto.ApiResult;
import club.banyuan.SpringBootHrms.model.Admin;
import club.banyuan.SpringBootHrms.repository.AdminDao;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AdminController {

    private AdminDao adminDao;

    @Autowired
    public AdminController(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    public AdminDao getAdminDao() {
        return adminDao;
    }

    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    /**
     * 获取当前登录的用户的信息
     * Current Admin
     * 通过url传过来的id去数据库中查询
     *
     * @param id
     * @return 返回一个map 存储了admin的三个属性
     */
    @GetMapping("/admins/{id}")
    public String adminInfo(@PathVariable Integer id) {
        Admin admin = adminDao.getAdminById(id);
        Map<String, String> map = new HashMap<>();
        map.put("username", admin.getUsername());
        map.put("rolename", admin.getRolename());
        map.put("password", admin.getPassword());
        return JSON.toJSONString(map);
    }

    /**
     * @param id
     * @param username
     * @param rolename
     * @param password
     * @return 返回成功的信息
     * @RequestParam 获取页面传来的属性
     * 调用数据更新的方法，更新当前数据
     */
    @PutMapping("/admins/{id}")
    public ApiResult<Admin> updateCurrentAdmin(@PathVariable Integer id,
                                               @RequestParam String username,
                                               @RequestParam String rolename,
                                               @RequestParam String password) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setUsername(username);
        admin.setRolename(rolename);
        admin.setPassword(password);

        adminDao.updateAdmin(admin);

        return ApiResult.success();
    }

    @GetMapping("/admins")
    public String adminList(@RequestParam String page, @RequestParam String rows) {
        Map<String, Object> map = new HashMap<>();
        List<Admin> admins = adminDao.getAdmins(Integer.parseInt(page), Integer.parseInt(rows));
        map.put("total", admins.size());
        map.put("code", 0);
        map.put("rows", admins);
        return JSON.toJSONString(map);
    }

    @PostMapping("/admins")
    public ApiResult<Admin> addAdmin(@RequestParam String username,
                                     @RequestParam String rolename,
                                     @RequestParam String password) {
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setRolename(rolename);
        admin.setPassword(password);

        adminDao.addAdmin(admin);

        return ApiResult.success();
    }

    @PostMapping("/admins/{id}")
    public ApiResult<Object> updateAdmin(@PathVariable String id,
                                         @RequestParam String username,
                                         @RequestParam String rolename,
                                         @RequestParam String password) {

        Admin admin = new Admin();
        admin.setId(Integer.parseInt(id));
        admin.setUsername(username);
        admin.setRolename(rolename);
        admin.setPassword(password);

        adminDao.updateAdmin(admin);
        return ApiResult.success();
    }

    @PostMapping("/admin/delete")
    public ApiResult<Admin> deleteAdmin(@RequestParam String ids) {
        String[] split = ids.split(",");
        Integer[] resultIds = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            resultIds[i] = Integer.parseInt(split[i]);
        }
        adminDao.deleteAdmin(resultIds);

        return ApiResult.success();
    }

}
